package com.wyp168.serviceedu.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyp168.commonutils.R;
import com.wyp168.serviceedu.entity.EduCourse;
import com.wyp168.serviceedu.entity.vo.CourseInfoVo;
import com.wyp168.serviceedu.entity.vo.CoursePublishVo;
import com.wyp168.serviceedu.entity.vo.CourseQuery;
import com.wyp168.serviceedu.service.EduCourseService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author wyp168
 * @since 2022-05-04
 */
@RestController
@RequestMapping("/serviceedu/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;



    /**
     * 添加课程基本方法
     */
    @PostMapping("addCourseInfo")
    public R addCourseInfo(
            @ApiParam("课程信息对象") @RequestBody CourseInfoVo courseInfoVo
    ) {
        String cid = courseService.saveCourseInfo(courseInfoVo);

        return R.ok().data("courseId",cid);
    }

    /**
     * 根据课程id查询课程基本信息
     */
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(
            @ApiParam("课程ID") @PathVariable String courseId
    ) {
        CourseInfoVo courseInfoVo = courseService.getCourseById(courseId);
        return R.ok().data("courseInfo",courseInfoVo);
    }

    //修改课程信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    //根据courseID链表查询课程基本信息
    @GetMapping("getPublishCourseInfo/{courseId}")
    public R getPublishCourseInfo(@PathVariable String courseId) {
        CoursePublishVo coursePublishVo = courseService.publishCourseInfo(courseId);
        System.out.println("******:" + coursePublishVo);
        return R.ok().data("publishCourse",coursePublishVo);
    }

    //修改课程发布状态：未发布 ---> 已发布Normal
    @PostMapping("updateCourseStatus/{courseId}")
    public R publishCourse(@PathVariable String courseId) {
        //创建一个Course对象，并设置其id和status属性，将之用来修改状态
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(courseId);
        eduCourse.setStatus("Normal");
        //调用Mapper方法，更新数据库
        courseService.updateById(eduCourse);
        return R.ok();
    }

    //条件查询课程列表带分页
    @PostMapping("coditionPageQueryCourseInfo/{pageNum}/{pageSize}")
    public R conditionPageQueryCourseInfo(
            @PathVariable Integer pageNum,
            @PathVariable Integer pageSize,
            @RequestBody CourseQuery courseQuery
    ) {
        Page<EduCourse> courseList = courseService.queryByConditionPage(pageNum,pageSize,courseQuery);

        return R.ok().data("courseList",courseList.getRecords()).data("total",courseList.getTotal());
    }

    //删除课程
    @DeleteMapping("deleteCourseInfoById/{courseId}")
    public R deleteById(@PathVariable String courseId) {
        courseService.deleteById(courseId);
        return R.ok();
    }
}

